#include "InputController.h"


InputController::InputController(void)
	:mLastKey(NULL)
	,mLastStatus(KeyIdle)
{}

InputController::~InputController(void)
{
}

void InputController::perform(Character* movingAgent, const Background* bg)
{
	if(mFacing != PROXIMITY_COUNT && mLastStatus == KeyPressed)
	{
		BackgroundElement* tileTo = movingAgent->mAdjacentTiles[mFacing];
		if(tileTo->isWalkable())
		{
			movingAgent->moveTo(bg, tileTo);
		}
	}
}

void InputController::reset()
{
	mLastKey	= NULL;
	mLastStatus = KeyIdle;
	mFacing		= PROXIMITY_COUNT;
}

//void InputController::seedMove(time_t nSeed)
//{
//	;
//}


void InputController::keyInput(const unsigned int Key, const KeyStatus status)
{	
	//if (I'm releasing a different key from which one I'm pressing)
	//		I'm assuming that the keyPressed has higher priority than KeyReleased
	if(!(mLastStatus == KeyPressed && status == KeyReleased && mLastKey != Key))
	{
		mLastKey	 = Key;
		mLastStatus  = status;
	}

	//char buffer[256]; 

	switch(mLastKey){
	case 0x26://UP_ARROW
	case 0x57://W
		//sprintf(buffer, "freccia SU %s\n",mLastStatus == KeyPressed ? "pressed." : "released."); 	
		setFacing(UP);
		break;
	case 0x25://LEFT_ARROW
	case 0x41://A
		//sprintf(buffer, "freccia SN %s\n",mLastStatus == KeyPressed ? "pressed." : "released."); 
		setFacing(LEFT);
		break;
	case 0x27://RIGHT_ARROW
	case 0x44: //D
		//sprintf(buffer, "freccia DX %s\n",mLastStatus == KeyPressed ? "pressed." : "released."); 
		setFacing(RIGHT);
		break;
	case 0x28://DOWN_ARROW
	case 0x53: //S
		//sprintf(buffer, "freccia GIU' %s\n",mLastStatus == KeyPressed ? "pressed." : "released."); 
		setFacing(DOWN);
		break;

	////the following cases are already taken care of in myEventHandler
	//case 0x50://P
	//case 0x13://pause
	//case 0x20://Space
	//	//sprintf(buffer, "Pause %s\n",mLastStatus == KeyPressed ? "pressed." : "released."); 
	//	break;
	//case 0x1b://esc
	//	//sprintf(buffer, "ESC %s\n",mLastStatus == KeyPressed ? "pressed." : "released."); 
	//	break;
	}
	//OutputDebugStringA(buffer); 
}